package com.task.submitter.controllers.images.transform;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskHandle;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.users.UserServiceFactory;
import com.task.submitter.dto.StoredImageDto;

@Controller
public class ImageLuckyController extends BaseImageTransformController {

	@RequestMapping("/task/image/lucky")
    public String images(StoredImageDto dto) throws IOException {
		String email = UserServiceFactory.getUserService().getCurrentUser().getEmail();
		TaskOptions options = TaskOptions.Builder.withUrl("/task/image/lucky/submit")
				.param("blobKey", dto.getBlobKey())
				.param("userName", email)
				.param("nameId", dto.getNameId())
				.param("transform", "LUCKY");
		TaskHandle handle = QueueFactory.getQueue("tasks").add(options);
		return "redirect:/task/image";
    }
	
	@ResponseBody
	@RequestMapping("/task/image/lucky/submit")
	@Override
    public void submit(StoredImageDto dto) throws IOException {
		super.submit(dto);	    
    }
	
	@Override
	public Image transform(Image oldImage) {
		Transform rotated = ImagesServiceFactory.makeImFeelingLucky();
	    return ImagesServiceFactory.getImagesService().applyTransform(rotated, oldImage);
	}
}
